using System;
using System.Reflection;

namespace SiliverlightJavascriptIntelli
{
    public class SilverlightEvent : IJavascriptConvertable
    {
        private string name;

        public SilverlightEvent(SilverlightType belongsTo)
        {
            this.belongsTo = belongsTo;
        }

        public static SilverlightEvent ConvertEventInfoToSilverlightEvent(EventInfo curEventInfoToConvert, SilverlightType belongsTo)
        {
            SilverlightEvent newEvent = new SilverlightEvent(belongsTo);

            newEvent.Name = curEventInfoToConvert.Name;
            newEvent.Description = ReferenceAnalyzer.GetDescriptionForEvent(newEvent.BelongsTo.Name, newEvent.Name);

            return newEvent;
        }

        private SilverlightType belongsTo;
        private string description;

        public SilverlightEvent(SilverlightType BelongsTo, string Name, string Description)
        {
            this.BelongsTo = BelongsTo;
            this.Name = Name;
            this.Description = Description;
        }

        public SilverlightType BelongsTo
        {
            get { return belongsTo; }
            set { belongsTo = value; }
        }

        public  string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public string ToJavascript()
        {
            return StringHelper.Format(JavascriptFormats.JavascriptEventFormat, this.BelongsTo.Name, this.Name, this.Description);
        }

        public override bool Equals(object obj)
        {
            if (obj is SilverlightEvent)
                return this.Name == ((SilverlightEvent)obj).Name;
            else
                return base.Equals(obj);
        }

        public static bool operator ==(SilverlightEvent x, SilverlightEvent y)
        {
            bool isEqual;
            if (object.ReferenceEquals(x, null))
            {
                if (object.ReferenceEquals(y, null))
                {
                    isEqual = true;
                }
                else
                {
                    isEqual = false;
                }
            }
            else
            {
                isEqual = x.Equals(y);
            }
            return isEqual;
        }


        public static bool operator !=(SilverlightEvent x, SilverlightEvent y)
        {
            return !(x == y);
        }
    }
}